<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
   <meta charset="UTF-8">
		<title>项目申报请求</title>
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
	    <meta name="description" content="Creative - Bootstrap 3 Responsive Admin Template">
	    <meta name="author" content="GeeksLabs">
	    <meta name="keyword" content="Creative, Dashboard, Admin, Template, Theme, Bootstrap, Responsive, Retina, Minimal">
	    <link rel="shortcut icon" href="img/favicon.png">

  </head>
  
  <body>
  
  		<c:import url="admin_home.jsp"></c:import>
  		<!-- 通过 Modal -->
    		<div class="modal fade bs-example-modal-lg" id="passModal" tabindex="-1" role="dialog" aria-labelledby="passModalLabel">
			  <div class="modal-dialog" style="margin-right: 1000px" role="document">
			    <div class="modal-content" style="width: 520px">
			      <div class="modal-header">
			        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
			        <h4 class="modal-title" id="myModalLabel">分配房间</h4>
			      </div>
			      <div class="modal-body">
			        
			        		<input type="hidden" id="passModal_projectId">
			        		<input type="hidden" id="passModal_memberNum">
			        	<table class="table table-hover">
						  <thead>
						  	<tr>
						  		<th>房间号</th>
						  		<!--  <th>已入驻项目</th>	-->
						  		<th>剩余空位</th>
						  		<th>选择</th>
						  	</tr>
						  </thead>
						  
						  <tbody id="roomInfo_table">
						  	
			        		
							  <c:forEach items="${list}" var="item"> 
							  	<tr>
							  		<td>${item.roomId}</td>
							  		<!-- <td>
							  			<c:forEach items="${item.projectName_room}" var="name">
							  				${name}&nbsp;
							  			</c:forEach>
							  		</td>	-->
							  		<td>${item.restPeople}</td>
							  		<td>
							  			
							  				<input type="radio" name="radio_roomId" value="${item.roomId}" onclick="clickRadio('${item.restPeople}')" title="分配">
							  			
							  		</td>
							  	</tr>
							  </c:forEach>
						  </tbody>
						</table>
			      </div>
			      <div class="modal-footer">
			        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
			        <button type="button" class="btn btn-primary" id="btn_successCheck">确定</button>
			      </div>
			    </div>
			  </div>
			</div>
			
			<!-- 驳回 Modal -->
    		<div class="modal fade bs-example-modal-lg" id="failModal" tabindex="-1" role="dialog" aria-labelledby="failModalLabel">
			  <div class="modal-dialog" style="margin-right: 1000px" role="document">
			    <div class="modal-content">
			      <div class="modal-header">
			        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
			        <h4 class="modal-title" id="myModalLabel">驳回申报请求</h4>
			      </div>
			      <div class="modal-body">
			        <form class="form-horizontal" id="fail_form">
			        <input type="hidden" id="failModal_projectId">
			        	<div class="form-group">
						    <label  class="col-sm-3 control-label">驳回理由:</label>
						    <div class="col-sm-9">
						      <textarea id="fail_form_reason" name="fail_reason" class="form-control" rows="5" placeholder="请描述驳回原因..."></textarea>
						    </div>
						  </div>
					</form>
			      </div>
			      <div class="modal-footer">
			        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
			        <button type="button" class="btn btn-primary" id="btn_failCheck">确定</button>
			      </div>
			    </div>
			  </div>
			</div>
     	<div class="layui-body container" style="margin-top: 100px">
     		<div class="layui-row" >
     			<form class="layui-form" action="#">
     				<div class="layui-form-item">
					      <div class="layui-input-inline layui-col-md-offset2 layui-col-md4">
					     	<input type="text" id="projectName" name="projectName" lay-verify="required" placeholder="输入项目名称" autocomplete="off" class="layui-input">
					      </div>
					      <div class="layui-col-md4">
					      	<button type="button" class="btn btn-info">查询</button>
					      		
					      </div>
					</div>
				</form>
     			
     		</div>
     	<!-- 显示表格数据 -->
     		<div class="layui-row">
	     		
     			<div class="layui-col-md-offset1 layui-col-md11">
     				<table class="table table-hover" id="teaTable">
     					<thead style="font-size: 15px">
     					<tr>
     						<th>ID</th>
     						<th>项目编号</th>
     						<th>项目名称</th>
     						<th>项目状态</th>
     						<th>申报人</th>
     						<th>申报成员数量</th>
     						<th>项目申报时间</th>
     						<th>预计完成时间</th>
     						<th>项目申报书</th>
     						<th>项目简介</th>
     						<th>操作</th>
     					</tr>
     					</thead>
     					<tbody>
     					
     					</tbody>
     					</table>
     			</div>
     		</div>
     		<div class="layui-row">
     			<div class="layui-col-md-offset1 layui-col-md3" id="page_info_area">
     				
     			</div>
     			<div class="col-md-5" id="page_info_nav">
     			</div>
     		</div>
     	</div>

		<script type="text/javascript">
		
		
	    $(function(){
	    	toPage(1);
	    });
	    function toPage(pn){
	    	$.ajax({
	    		url:"${pageContext.request.contextPath}/admin/applyProjectInfo.do",
	    		data:"pageNum="+pn,
	    		type:"GET",
	    		success:function(result){
	    			//console.log(result);
	    			//1.解析并显示数据
	    			build_tableInfo(result);
	    			//2.解析显示分页信息
	    			build_pageInfo(result);
	    			build_page_nav(result);
	    			
	    		}
	    	});
	    }
	    function show_projectInfo(info){
	    	console.log(info);
	    	
	    	 layer.alert(info, {
	    		    skin: 'layui-layer-lan'
	    		    ,title:'项目描述'
	    		    ,closeBtn: 0
	    		    ,anim: 4 //动画类型
	    		  });
	    }
	    //解析显示分页信息
	    function build_tableInfo(result){
	    	$("#teaTable tbody").empty();
	    	var applyInfo=result.extend.page.list;
	    	$.each(applyInfo,function(index,pro){
	    		var id=$("<td></td>").append(++index);
	    		var projectId=$("<td></td>").append(pro.projectId);
	    		var projectName=$("<td></td>").append(pro.projectName);
	    		
	    		var status="待定";
	    		if(pro.projectStatus==1){
	    			status="已通过";
	    		}else if(pro.projectStatus==2){
	    			status="已驳回";
	    		}else if(pro.projectStatus==3){
	    			status="验收完成";
	    		}
	    		var projectStatus=$("<td></td>").append(status);
	    		var leaderName=$("<td></td>").append(pro.leaderName);
	    		var memberNum=$("<td></td>").append(pro.memberNum);
	    		var applyTime=$("<td></td>").append(pro.applyTime);
	    		var finishTime=$("<td></td>").append(pro.finishTime);
	    		var declareReport=$("<td></td>").append($("<a></a>")
	    				.attr("href","${pageContext.request.contextPath}/file/downloadProjectFile.do?fileId="+pro.declareReport)
	    				.append("下载"));
	    	
	    		var projectInfo=$("<td></td>").append($("<a></a>").attr("href","javascript:void(0)")
	    				.attr("onclick","show_projectInfo('"+pro.projectInfo+"')").append("查看"));
	    		var editTd;
	    		if(pro.projectStatus==0){
	    			//var aPass=$("<a></a>").attr("href","${pageContext.request.contextPath}/admin/editProjectStatus.do?projectId="+pro.projectId+"&projectStatus="+1).append(" 通过 ");
	    			//var a2=$("<a></a>").attr("href","${pageContext.request.contextPath}/admin/editProjectStatus.do?projectId="+pro.projectId+"&projectStatus="+2).append(" 驳回 ");
	    			var a_pass=$("<a></a>").attr("href","javascript:void(0)").attr("onclick","edit_apply("+pro.projectId+","+pro.memberNum+",1)").append("通过");
	    			var a_false=$("<a></a>").attr("href","javascript:void(0)").attr("onclick","edit_apply("+pro.projectId+","+pro.memberNum+",2)").append("驳回");
	    			editTd=$("<td></td>").append(a_pass).append("/").append(a_false);
	    		}else if(pro.projectStatus==1){
	    			editTd=$("<td></td>").append("已通过");
	    		}else if(pro.projectStatus==2){
	    			var a_fail=$("<a></a>").attr("href","javascrip:void(0)").attr("onclick","show_failReason('"+pro.fail_reason+"')").append("驳回原因");
	    			editTd=$("<td></td>").append(a_fail);
	    		}else if(pro.projectStatus==3){
	    			editTd=$("<td></td>").append("验收完成");
	    		}
	    		$("<tr></tr>").append(id).append(projectId).append(projectName).append(projectStatus)
	    					.append(leaderName).append(memberNum).append(applyTime).append(finishTime).append(declareReport)
	    					.append(projectInfo).append(editTd).appendTo("#teaTable tbody");
	    	});
	    }
	    $("#projectInfo1").click(function(){
	    	layer.alert("info");
	    });
	    
	    //解析显示分页信息
	    var currentPage;
	    function build_pageInfo(result){
	    	$("#page_info_area").empty();
	    	currentPage=result.extend.page.pageNum;
	    	$("#page_info_area").append("当前"+result.extend.page.pageNum+"页，共"+result.extend.page.pages+"页,一共"+result.extend.page.total+"条记录");
	    	totalRecord=result.extend.page.total;
	    }
	    
	    function build_page_nav(result){
	    	$("#page_info_nav").empty();
	    	var ul=$("<ul></ul>").addClass("pagination");
	    	var firstPage=$("<li></li>").append($("<a></a>").append("首页").attr("href","#"));
	    	var prePage=$("<li></li>").append($("<a></a>").append("上一页"));
			if(result.extend.page.hasPreviousPage==false){
				firstPage.addClass("disabled");  
				prePage.addClass("prePage");
	    	}
			firstPage.click(function(){
				toPage(1);
			});
			prePage.click(function(){
				if(result.extend.page.pageNum==1){
					toPage(1);
				}else{
					toPage(result.extend.page.pageNum-1);
				}
				
			});
	    	var nextPage=$("<li></li>").append($("<a></a>").append("下一页"));
	    	var lastPage=$("<li></li>").append($("<a></a>").append("尾页").attr("href","#"));
	    	if(result.extend.page.hasNextPage==false){
	    		nextPage.addClass("disabled");  
	    		lastPage.addClass("prePage");
	    	}
	    	lastPage.click(function(){
				toPage(result.extend.page.pages);
			});
	    	nextPage.click(function(){
	    		if(result.extend.page.pageNum==result.extend.page.pages){
	    			toPage(result.extend.page.pages);
	    		}else{
	    			toPage(result.extend.page.pageNum+1);
	    		}
				
			});
	    	ul.append(firstPage).append(prePage);
	    	$.each(result.extend.page.navigatepageNums,function(index,item){
	    		var numLi=$("<li></li>").append($("<a></a>").append(item));
	    		if(result.extend.page.pageNum==item){
	    			numLi.addClass("active");
	    		}
	    		numLi.click(function(){
	    			toPage(item);
	    		});
	    		ul.append(numLi);
	    	});
	    	ul.append(nextPage).append(lastPage);
	    	var navElv=$("<nv></nv>").append(ul);
	    	navElv.appendTo("#page_info_nav");
	    }
	    //删除
	    $(document).on("click",".delete_btn",function(){
	    	var teaId=$(this).parents("tr").find("td:eq(1)").text();
	    	if(confirm("确认删除工号为："+teaId+"的教师吗？")){
	    		$.ajax({
	    			url:"${pageContext.request.contextPath}/admin/deleteTea.do",
	    			data:"teaId="+teaId,
	    			type:"GET",
	    			success:function(result){
	    				console.log("result:"+result);
	    				toPage(currentPage);
	    			}
	    		});
	    	}
	    });
	    
	    //通过,驳回操作
	    function edit_apply(projectId,memberNum,status){
	    	if(status==1){//通过
	    		console.log(projectId+"_"+memberNum+"_"+status);
	    		$("#passModal_projectId").val(projectId);
	    		$("#passModal_memberNum").val(memberNum);
	    		$("#roomInfo_table").append();
	    		$("#passModal").modal("show");
	    		
	    	}else if(status==2){//驳回
	    		$("#failModal_projectId").val(projectId);
	    		$("#failModal").modal("show");
	    	}
	    }
	    function show_failReason(info){
	    	layer.alert(info, {
    		    skin: 'layui-layer-lan'
    		    ,title:'验收要求'
    		    ,closeBtn: 0
    		    ,anim: 1 //动画类型
    		  });
	    }
	    //选中单选框事件
	    function clickRadio(restNum) {
	    	var memberNum=$("#passModal_memberNum").val();
			if(memberNum>restNum){
				$("input[name=radio_roomId]").attr("checked",false);
				layer.msg('此房间空位不足', {icon: 5,anim:6,time:1000});
			}
		}
	    //确定通过按钮
	   	$("#btn_successCheck").click(function(){
	   		var projectId=$("#passModal_projectId").val();
	   		var roomId=$("input[name='radio_roomId']:checked").val();
	   		var memberNum=$("#passModal_memberNum").val();
	   		console.log(projectId+","+roomId+","+memberNum);
	   		if(roomId==null){
	   			layer.msg('请先选择所分配的房间', {icon: 5,anim:6,time:1000});
	   			return;
	   		}
	   		layer.confirm('确定通过ID为'+projectId+'的项目，并为其分配'+roomId+'房间吗？', {
	   		  btn: ['确定','取消'] //按钮
	   		}, function(){
	   			$("#passModal").modal("hide");
		   		  $.ajax({
		   			  url:"${pageContext.request.contextPath}/admin/sucessProjectStatus.do",
		   			  data:{"projectId":projectId,"projectStatus":1,"roomId":roomId},
		   			  type:"GET",
		   			  success:function(result){
		   				  console.log(result);
		   				  if(result.code=100){
		   					layer.msg('操作成功!', {icon: 1,time:2000,end:function(){
								location.reload();
							}});
		   				  }else{
		   					layer.msg('操作失败!', {icon: 2,time:2000,end:function(){
								location.reload();
							}});
		   				  }
		   			  }
		   		  });
	   		}, function(){
	   		  //取消
	   		});
	   		
	   	});
	    //确定驳回按钮
	    $("#btn_failCheck").click(function(){
	    	var projectId=$("#failModal_projectId").val();
	    	var fail_reason=$("#fail_form_reason").val();
	    	layer.confirm('确定驳回此项目吗？', {
		   		  btn: ['确定','取消'] //按钮
		   		},function(){
		   			$("#failModal").modal("hide");
		   			$.ajax({
				    	url:"${pageContext.request.contextPath}/admin/failProjectStatus.do",
				    	data:{"projectId":projectId,"fail_reason":fail_reason},
				    	type:"GET",
				    	success:function(result){
				    		console.log(result);
				    		if(result.code=100){
				    			layer.msg('操作成功!', {icon: 1,time:2000,end:function(){
									location.reload();
								}});
				    		}
				    		
				    	}
				    });
		   		},function(){
		   			
		   	});
	    	
	    	
	    });
	    
		//layui
		layui.use('form', function(){
		  var form = layui.form;
		  
		});
		
	  </script>
  </body>
</html>
